/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvAdministratifvehicule;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvVehicule;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
 public class CvAdministratifvehiculeJpaController extends DaoImp implements Serializable {

    public CvAdministratifvehiculeJpaController() {
        super();
    }
    public void create(CvAdministratifvehicule cvAdministratifvehicule) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        CvVehicule cvVehiculeOrphanCheck = cvAdministratifvehicule.getCvVehicule();
        if (cvVehiculeOrphanCheck != null) {
            CvAdministratifvehicule oldCvAdministratifvehiculeOfCvVehicule = cvVehiculeOrphanCheck.getCvAdministratifvehicule();
            if (oldCvAdministratifvehiculeOfCvVehicule != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The CvVehicule " + cvVehiculeOrphanCheck + " already has an item of type CvAdministratifvehicule whose cvVehicule column cannot be null. Please make another selection for the cvVehicule field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvVehicule cvVehicule = cvAdministratifvehicule.getCvVehicule();
            if (cvVehicule != null) {
                cvVehicule = em.getReference(cvVehicule.getClass(), cvVehicule.getIdvehicule());
                cvAdministratifvehicule.setCvVehicule(cvVehicule);
            }
            em.persist(cvAdministratifvehicule);
            if (cvVehicule != null) {
                cvVehicule.setCvAdministratifvehicule(cvAdministratifvehicule);
                cvVehicule = em.merge(cvVehicule);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvAdministratifvehicule(cvAdministratifvehicule.getIdvehicule()) != null) {
                throw new PreexistingEntityException("CvAdministratifvehicule " + cvAdministratifvehicule + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvAdministratifvehicule cvAdministratifvehicule) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvAdministratifvehicule persistentCvAdministratifvehicule = em.find(CvAdministratifvehicule.class, cvAdministratifvehicule.getIdvehicule());
            CvVehicule cvVehiculeOld = persistentCvAdministratifvehicule.getCvVehicule();
            CvVehicule cvVehiculeNew = cvAdministratifvehicule.getCvVehicule();
            List<String> illegalOrphanMessages = null;
            if (cvVehiculeNew != null && !cvVehiculeNew.equals(cvVehiculeOld)) {
                CvAdministratifvehicule oldCvAdministratifvehiculeOfCvVehicule = cvVehiculeNew.getCvAdministratifvehicule();
                if (oldCvAdministratifvehiculeOfCvVehicule != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The CvVehicule " + cvVehiculeNew + " already has an item of type CvAdministratifvehicule whose cvVehicule column cannot be null. Please make another selection for the cvVehicule field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (cvVehiculeNew != null) {
                cvVehiculeNew = em.getReference(cvVehiculeNew.getClass(), cvVehiculeNew.getIdvehicule());
                cvAdministratifvehicule.setCvVehicule(cvVehiculeNew);
            }
            cvAdministratifvehicule = em.merge(cvAdministratifvehicule);
            if (cvVehiculeOld != null && !cvVehiculeOld.equals(cvVehiculeNew)) {
                cvVehiculeOld.setCvAdministratifvehicule(null);
                cvVehiculeOld = em.merge(cvVehiculeOld);
            }
            if (cvVehiculeNew != null && !cvVehiculeNew.equals(cvVehiculeOld)) {
                cvVehiculeNew.setCvAdministratifvehicule(cvAdministratifvehicule);
                cvVehiculeNew = em.merge(cvVehiculeNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvAdministratifvehicule.getIdvehicule();
                if (findCvAdministratifvehicule(id) == null) {
                    throw new NonexistentEntityException("The cvAdministratifvehicule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvAdministratifvehicule cvAdministratifvehicule;
            try {
                cvAdministratifvehicule = em.getReference(CvAdministratifvehicule.class, id);
                cvAdministratifvehicule.getIdvehicule();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvAdministratifvehicule with id " + id + " no longer exists.", enfe);
            }
            CvVehicule cvVehicule = cvAdministratifvehicule.getCvVehicule();
            if (cvVehicule != null) {
                cvVehicule.setCvAdministratifvehicule(null);
                cvVehicule = em.merge(cvVehicule);
            }
            em.remove(cvAdministratifvehicule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvAdministratifvehicule> findCvAdministratifvehiculeEntities() {
        return findCvAdministratifvehiculeEntities(true, -1, -1);
    }

    public List<CvAdministratifvehicule> findCvAdministratifvehiculeEntities(int maxResults, int firstResult) {
        return findCvAdministratifvehiculeEntities(false, maxResults, firstResult);
    }

    private List<CvAdministratifvehicule> findCvAdministratifvehiculeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvAdministratifvehicule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvAdministratifvehicule findCvAdministratifvehicule(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvAdministratifvehicule.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvAdministratifvehiculeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvAdministratifvehicule> rt = cq.from(CvAdministratifvehicule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
